package com.lhh.jiu.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhh.jiu.entity.Admin;
import com.lhh.jiu.mapper.AdminMapper;
import com.lhh.jiu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired(required = false)
    private AdminMapper adminMapper;

    //条件分页查询
    @Override
    public IPage<Admin> selectAdmin(int pageNum, int pageSize, String name,String time) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            queryWrapper.like("name",name);
        }
        if(StringUtils.isNotBlank(time)){
            queryWrapper.like("wtime",time);
        }
        Page<Admin> page = new Page<>(pageNum,pageSize);
        return adminMapper.selectPage(page,queryWrapper);
    }

    //新增
    @Override
    public int addAdmin(Admin admin) {
        return adminMapper.insert(admin);
    }

    //修改
    @Override
    public int editAdmin(Admin admin) {
        return adminMapper.updateById(admin);
    }

    //主键查询
    @Override
    public Admin queryAdmin(Integer id) {
        return adminMapper.selectById(id);
    }

    //主键删除
    @Override
    public int delAdmin(Integer id) {
        return adminMapper.deleteById(id);
    }

    //查询全部，不分页
    @Override
    public List<Admin> queryAll() {
        return adminMapper.selectList(null);
    }

    @Override
    public List<Admin> queryNameAndTime(String name,String wtime) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(name!=null){
            queryWrapper.like("name",name);
        }
        if(wtime!=null){
            queryWrapper.like("wtime",wtime);
        }
        List<Admin> list = adminMapper.selectList(queryWrapper);
        System.out.println(list);
        return  list;
    }

    @Override
    public void AddOrUpdate(Admin admin) {
        System.out.println(admin);
        if(admin.getId()==null){
            addAdmin(admin);
        }else {
            editAdmin(admin);
        }
    }


}
